﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using StockAdjust;

namespace PresentationLayer
{
    public partial class ApproveStockAdjustmentVoucherList : System.Web.UI.Page
    {
        ApproveStockAdjustmentVoucherControl control;
        User user;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId < 6 || roleId > 7)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                control = new ApproveStockAdjustmentVoucherControl();

                List<string> userList = Session["UserLoggedIn"] as List<string>;
                string username = userList[3];
                user = control.GetUserByUsername(username);

                // Block the buttons to prevent multiple clicks by the user
                btnApprove.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnApprove).ToString() + ";");

                if (!IsPostBack)
                {
                    lblMessage.Text = "";
                    GenerateGridView(user);
                }
            }
            catch (Exception ex)
            {
                btnApprove.Enabled = true;
            }
        }

        private void GenerateGridView(User user)
        {
            List<AdjustmentDisplay> list = new List<AdjustmentDisplay>();
            if (user.Role.RoleName.Equals("Store Manager"))
            {
                list = control.GetAllPendingAdjustmentForStoreManager();
            }
            else if (user.Role.RoleName.Equals("Store Supervisor"))
            {
                list = control.GetAllPendingAdjustmentForSupervisor();
            }
            if (list.Count > 0)
            {
                gvApproveList.DataSource = list;
                gvApproveList.DataBind();
            }
            else
            {
                lblMessage.Text = "There is no pending stock adjustment voucher at the moment";
                gvApproveList.Visible = false;
                btnApprove.Visible = false;
            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            try
            {
                // Loop through the gridview to find the rows that are checked
                List<int> voucherList = new List<int>();
                foreach (GridViewRow row in gvApproveList.Rows)
                {
                    CheckBox checkbox = (CheckBox)row.FindControl("cboVoucher");
                    if (checkbox.Checked)
                    {
                        Label voucherid = (Label)row.FindControl("lblVoucherId") as Label;
                        voucherList.Add(Convert.ToInt32(voucherid.Text));
                    }
                }

                if (voucherList.Count > 0)
                {
                    // Update ApprovalBy, ApprovalDate and Status to database
                    string redirectURL = control.UpdateVouchers(user, voucherList);

                    // Save to HttpSession
                    Session["AdjustmentDetail"] = voucherList;

                    // Redirect to successful page
                    Response.Redirect(redirectURL);
                }
                else
                {
                    lblMessage.Text = "No stock adjustment voucher is selected";
                }
            }
            catch (Exception ex)
            {
                // Release blocked button
                btnApprove.Enabled = true;
            }
        }

        protected void lbtnViewDetail_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
                Label lblVoucherId = (Label)clickedRow.FindControl("lblVoucherId");
                List<int> voucherId = new List<int>();
                voucherId.Add(Convert.ToInt32(lblVoucherId.Text));

                // Save to HttpSession
                Session["AdjustmentDetail"] = voucherId;

                // Redirect to view details page
                string redirectURL = control.ViewAdjustmentDetail();
                Response.Redirect(redirectURL);
            }
            catch (Exception ex)
            {
            }
        }

    }
}